
#include <BFont>
#include <BWidget>
#include "member_BMenu.h"

using namespace BWE;

member_BMenu::member_BMenu(BMenu* menu)
{
	boss = menu;
	space = 1;
}
member_BMenu::~member_BMenu()
{

}

void member_BMenu::procTextChanged(BObject* object, const BValue& value)
{
	adjustRects();
}

void member_BMenu::adjustRects()
{
	const BFont* font = boss->font();
	if (!font)
		return;

	int border = boss->border();
	BMargins margin = boss->margins();
	BRect crect = boss->clientRect();
	int cx = crect.x();
	int cy = crect.y();
	int cw = crect.width();
	int ch = crect.height();

	int maxWidth = 0;
	int posX = cx;
	int posY = cy;
	for(int i = 0; i < actions.size(); i++)
	{
		const BString& text = actions[i].action->text();
		BSize textSize = font->textSize(text);
		int width = space + textSize.width();
		maxWidth = bMax(maxWidth, width);
		actions[i].rect.set(posX, posY, width, textSize.height());
		posY += textSize.height() + space;
	}
	for (int i = 0; i < actions.size(); i++)
	{
		actions[i].rect.setWidth(maxWidth);
	}

	boss->setClientSize(maxWidth, posY);
}
